﻿using GEPL.InterfaceLibrary;
using GEPL.StandardModulesLibrary.Models;
using GEPL.StandardModulesLibrary.ViewModels.Common;
using GEPL.StandardModulesLibrary.Views.Common;
using System;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Text;

namespace GEPL.StandardModulesLibrary.Elements.Common
{
    [Serializable]
    public class SetVariableElement : IElement
    {
        private readonly SetVariableViewModel viewModel;

        public SetVariableElement()
        {
            viewModel = new SetVariableViewModel();
        }

        public SetVariableElement(SerializationInfo info, StreamingContext context)
        {
            viewModel = new SetVariableViewModel()
            {
                VariablesList = (ObservableCollection<Variable>)info.GetValue(nameof(viewModel.VariablesList), typeof(ObservableCollection<Variable>))
            };
        }

        public ElementType ElementType
        {
            get { return ElementType.NormalStatement; }
        }

        public ProjectType ProjectType
        {
            get { return ProjectType.Any; }
        }

        public string ElementName
        {
            get { return Properties.Resources.Names_SetVariable; }
        }

        public object Data
        {
            get { return viewModel; }
        }

        public Type UserControlType
        {
            get { return typeof(SetVariableView); }
        }

        public string GetScript()
        {
            var sb = new StringBuilder();

            foreach (var variable in viewModel.VariablesList)
            {
                if (String.IsNullOrWhiteSpace(variable.Name) || String.IsNullOrWhiteSpace(variable.Value))
                    continue;
                sb.AppendFormat("{0} = {1}\n", variable.Name, variable.Value);
            }

            return sb.ToString();
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(nameof(viewModel.VariablesList), viewModel.VariablesList, typeof(ObservableCollection<Variable>));
        }
    }
}